<?php
/**
 * VIP权益图标上传功能测试文件
 */

// 测试上传接口
function testUpload() {
    $url = 'http://localhost:8787/admin/upload/do';
    
    // 创建一个测试图片文件
    $testImagePath = 'test_icon.png';
    if (!file_exists($testImagePath)) {
        // 创建一个简单的测试图片
        $image = imagecreate(40, 40);
        $bgColor = imagecolorallocate($image, 255, 255, 255);
        $textColor = imagecolorallocate($image, 0, 0, 0);
        imagestring($image, 5, 5, 15, 'TEST', $textColor);
        imagepng($image, $testImagePath);
        imagedestroy($image);
    }
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'file' => new CURLFile($testImagePath)
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer your_token_here' // 需要替换为实际的token
    ]);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo "上传接口测试:\n";
    echo "HTTP状态码: $httpCode\n";
    echo "响应内容: $response\n\n";
    
    // 清理测试文件
    if (file_exists($testImagePath)) {
        unlink($testImagePath);
    }
}

// 测试完整的VIP权益创建流程（包含上传）
function testVipDetailWithUpload() {
    echo "开始测试VIP权益创建流程（包含图标上传）...\n\n";
    
    // 1. 先上传图标
    echo "步骤1: 上传图标文件\n";
    testUpload();
    
    // 2. 创建VIP权益（使用上传后的路径）
    echo "步骤2: 创建VIP权益\n";
    $url = 'http://localhost:8787/admin/vipSolution/submitVipDetail';
    $data = [
        'icon' => '/uploads/vip/test_icon.png', // 假设上传后的路径
        'name' => '测试权益（带图标）',
        'sub_name' => '测试副标题',
        'description' => '这是一个带图标的测试权益'
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer your_token_here' // 需要替换为实际的token
    ]);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo "创建VIP权益结果:\n";
    echo "HTTP状态码: $httpCode\n";
    echo "响应内容: $response\n\n";
}

// 运行测试
echo "开始测试VIP权益图标上传功能...\n\n";

echo "注意：请确保：\n";
echo "1. 后端服务已启动 (php start.php start)\n";
echo "2. 已登录并获取有效的token\n";
echo "3. 上传目录有写入权限\n";
echo "4. 数据库迁移已运行 (php phinx.php migrate)\n\n";

// testUpload();
// testVipDetailWithUpload();

echo "测试完成！\n";
echo "提示：取消注释上面的测试函数来运行实际测试\n";
?> 